<?php

include_once(dirname(__FILE__) . "../../_DATABASE_DAO/GenericDAO.class.php");
$genericDAO = new GenericDAO();

if($_GET["action"]=="save"){
    if(isset($_POST["email"]) && isset($_POST["name"]) && isset($_POST["company"]) && isset($_POST["mobile"])){
        $ss = $genericDAO->getWhere("sponsor", "email='".$_POST["email"]."'");
        if(isset( $ss-> email) && $ss->email == $_POST["email"])
        {
            echo "<h1>Please Don't Re-post the Form</h1>";
            exit;
        }
        $sponsor = $genericDAO->save("sponsor", array(
            "name"=>$_POST['name'],
            "company"=>$_POST['company'],
            "title"=>$_POST["title"],
            "email"=>$_POST["email"],
            "mobile"=>$_POST["mobile"],
            "comment"=>$_POST["comment"]
            ));
        $interestedQuestionIds = $_POST["interestedQuestionId"];

        if( isset($_REQUEST['other']) ){
                $genericDAO->save("sponsorQuestion", array("sponsorId"=>$sponsor,"interestQuestionId"=>1,"others"=>$_REQUEST["other"]));
        }
        foreach($interestedQuestionIds as $interestedQuestionId){
                if($interestedQuestionId!="xx")
                $genericDAO->save("sponsorQuestion", array("sponsorId"=>$sponsor,"interestQuestionId"=>$interestedQuestionId));
        }

        $privileges = $_POST["privilage"];
        foreach($privileges as $privilege ){
            $genericDAO->save("sponsorPrivileges", array("sponsorId"=>$sponsor,"privilegeId"=>$privilege));
            $p = $genericDAO->get("privileges", $privilege);
            $genericDAO->edit("privileges", array("usedQuantity" => ($p->usedQuantity+1) ) , "id = ".$privilege);
        }
    }
    include 'views/index/thanks.php';
    
}else{
    $privilegesTypes = $genericDAO->listAllWhere("privilegesType", "isDeleted =0");
    foreach($privilegesTypes as $privilegesType){
        $privilegesType->privilages = $genericDAO->listAllWhere("privileges", "isDeleted =0");
    }

    $basicPackages = $genericDAO->listAllWhere("basicPackage", "isDeleted =0");
    $interestedQuestions = $genericDAO->listAllWhere("interestedQuestion", "isDeleted =0");

     include ('views/index/index.php');

}
?>